home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / tcl / tcl70b2.lha / tcl7.0b2 / tests / parse.test < prev    next >
Text File  |  1993-02-06  |  12KB  |  427 lines

  1. # Commands covered:  set (plus basic command syntax)
  2. #
  3. # This file contains a collection of tests for one or more of the Tcl
  4. # built-in commands.  Sourcing this file into Tcl runs the tests and
  5. # generates output for errors.  No output means no errors were found.
  6. #
  7. # Copyright (c) 1991-1993 The Regents of the University of California.
  8. # All rights reserved.
  9. #
  10. # Permission is hereby granted, without written agreement and without
  11. # license or royalty fees, to use, copy, modify, and distribute this
  12. # software and its documentation for any purpose, provided that the
  13. # above copyright notice and the following two paragraphs appear in
  14. # all copies of this software.
  15. #
  16. # IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  17. # DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  18. # OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  19. # CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  20. #
  21. # THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  22. # INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  23. # AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  24. # ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  25. # PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  26. #
  27. # $Header: /user6/ouster/tcl/tests/RCS/parse.test,v 1.23 93/02/06 15:54:02 ouster Exp $ (Berkeley)
  28.  
  29. if {[string compare test [info procs test]] == 1} then {source defs}
  30.  
  31. proc fourArgs {a b c d} {
  32.     global arg1 arg2 arg3 arg4
  33.     set arg1 $a
  34.     set arg2 $b
  35.     set arg3 $c
  36.     set arg4 $d
  37. }
  38.  
  39. proc getArgs args {
  40.     global argv
  41.     set argv $args
  42. }
  43.  
  44. # Basic argument parsing.
  45.  
  46. test parse-1.1 {basic argument parsing} {
  47.     set arg1 {}
  48.     fourArgs a b    c          d
  49.     list $arg1 $arg2 $arg3 $arg4
  50. } {a b c d}
  51. test parse-1.2 {basic argument parsing} {
  52.     set arg1 {}
  53.     eval "fourArgs 123\v4\f56\r7890"
  54.     list $arg1 $arg2 $arg3 $arg4
  55. } {123 4 56 7890}
  56.  
  57. # Quotes.
  58.  
  59. test parse-2.1 {quotes and variable-substitution} {
  60.     getArgs "a b c" d
  61.     set argv
  62. } {{a b c} d}
  63. test parse-2.2 {quotes and variable-substitution} {
  64.     set a 101
  65.     getArgs "a$a b c"
  66.     set argv
  67. } {{a101 b c}}
  68. test parse-2.3 {quotes and variable-substitution} {
  69.     set argv "xy[format xabc]"
  70.     set argv
  71. } {xyxabc}
  72. test parse-2.4 {quotes and variable-substitution} {
  73.     set argv "xy\t"
  74.     set argv
  75. } xy\t
  76. test parse-2.5 {quotes and variable-substitution} {
  77.     set argv "a b    c
  78. d e f"
  79.     set argv
  80. } a\ b\tc\nd\ e\ f
  81. test parse-2.6 {quotes and variable-substitution} {
  82.     set argv a"bcd"e
  83.     set argv
  84. } {a"bcd"e}
  85.  
  86. # Braces.
  87.  
  88. test parse-3.1 {braces} {
  89.     getArgs {a b c} d
  90.     set argv
  91. } "{a b c} d"
  92. test parse-3.2 {braces} {
  93.     set a 101
  94.     set argv {a$a b c}
  95.     set b [string index $argv 1]
  96.     set b
  97. } {$}
  98. test parse-3.3 {braces} {
  99.     set argv {a[format xyz] b}
  100.     string length $argv
  101. } 15
  102. test parse-3.4 {braces} {
  103.     set argv {a\nb\}}
  104.     string length $argv
  105. } 6
  106. test parse-3.5 {braces} {
  107.     set argv {{{{}}}}
  108.     set argv
  109. } "{{{}}}"
  110. test parse-3.6 {braces} {
  111.     set argv a{{}}b
  112.     set argv
  113. } "a{{}}b"
  114. test parse-3.7 {braces} {
  115.     set a [format "last]"]
  116.     set a
  117. } {last]}
  118.  
  119. # Command substitution.
  120.  
  121. test parse-4.1 {command substitution} {
  122.     set a [format xyz]
  123.     set a
  124. } xyz
  125. test parse-4.2 {command substitution} {
  126.     set a a[format xyz]b[format q]
  127.     set a
  128. } axyzbq
  129. test parse-4.3 {command substitution} {
  130.     set a a[
  131. set b 22;
  132. format %s $b
  133.  
  134. ]b
  135.     set a
  136. } a22b
  137.  
  138. # Variable substitution.
  139.  
  140. test parse-5.1 {variable substitution} {
  141.     set a 123
  142.     set b $a
  143.     set b
  144. } 123
  145. test parse-5.2 {variable substitution} {
  146.     set a 345
  147.     set b x$a.b
  148.     set b
  149. } x345.b
  150. test parse-5.3 {variable substitution} {
  151.     set _123z xx
  152.     set b $_123z^
  153.     set b
  154. } xx^
  155. test parse-5.4 {variable substitution} {
  156.     set a 78
  157.     set b a${a}b
  158.     set b
  159. } a78b
  160. test parse-5.5 {variable substitution} {catch {$_non_existent_} msg} 1
  161. test parse-5.6 {variable substitution} {
  162.     catch {$_non_existent_} msg
  163.     set msg
  164. } {can't read "_non_existent_": no such variable}
  165. test parse-5.7 {array variable substitution} {
  166.     catch {unset a}
  167.     set a(xyz) 123
  168.     set b $a(xyz)foo
  169.     set b
  170. } 123foo
  171. test parse-5.8 {array variable substitution} {
  172.     catch {unset a}
  173.     set "a(x y z)" 123
  174.     set b $a(x y z)foo
  175.     set b
  176. } 123foo
  177. test parse-5.9 {array variable substitution} {
  178.     catch {unset a}; catch {unset qqq}
  179.     set "a(x y z)" qqq
  180.     set $a([format x]\ y [format z]) foo
  181.     set qqq
  182. } foo
  183. test parse-5.10 {array variable substitution} {
  184.     catch {unset a}
  185.     list [catch {set b $a(22)} msg] $msg
  186. } {1 {can't read "a(22)": no such variable}}
  187. test parse-5.11 {array variable substitution} {
  188.     set b a$!
  189.     set b
  190. } {a$!}
  191. test parse-5.12 {array variable substitution} {
  192.     set b a$()
  193.     set b
  194. } {a$()}
  195. catch {unset a}
  196. test parse-5.13 {array variable substitution} {
  197.     catch {unset a}
  198.     set long {This is a very long variable, long enough to cause storage \
  199.     allocation to occur in Tcl_ParseVar.  If that storage isn't getting \
  200.     freed up correctly, then a core leak will occur when this test is \
  201.     run.  This text is probably beginning to sound like drivel, but I've \
  202.     run out of things to say and I need more characters still.}
  203.     set a($long) 777
  204.     set b $a($long)
  205.     list $b [array names a]
  206. } {777 {{This is a very long variable, long enough to cause storage \
  207.     allocation to occur in Tcl_ParseVar.  If that storage isn't getting \
  208.     freed up correctly, then a core leak will occur when this test is \
  209.     run.  This text is probably beginning to sound like drivel, but I've \
  210.     run out of things to say and I need more characters still.}}}
  211. test parse-5.14 {array variable substitution} {
  212.     catch {unset a}; catch {unset b}; catch {unset a1}
  213.     set a1(22) foo
  214.     set a(foo) bar
  215.     set b $a($a1(22))
  216.     set b
  217. } bar
  218. catch {unset a}; catch {unset a1}
  219.  
  220. # Backslash substitution.
  221.  
  222. set errNum 1
  223. proc bsCheck {char num} {
  224.     global errNum
  225.     test parse-6.$errNum {backslash substitution} {
  226.     scan $char %c value
  227.     set value
  228.     } $num
  229.     set errNum [expr $errNum+1]
  230. }
  231.  
  232. bsCheck \b    8
  233. bsCheck \e    101
  234. bsCheck \f    12
  235. bsCheck \n    10
  236. bsCheck \r    13
  237. bsCheck \t    9
  238. bsCheck \v    11
  239. bsCheck \{    123
  240. bsCheck \}    125
  241. bsCheck \[    91
  242. bsCheck \]    93
  243. bsCheck \$    36
  244. bsCheck \     32
  245. bsCheck \;    59
  246. bsCheck \\    92
  247. bsCheck \Ca    67
  248. bsCheck \Ma    77
  249. bsCheck \CMa    67
  250. bsCheck \8a    8
  251. bsCheck \14    12
  252. bsCheck \141    97
  253. bsCheck \340    224
  254. bsCheck b\0    98
  255. bsCheck \x    120
  256. bsCheck \xa    10
  257. bsCheck \x41    65
  258. bsCheck \x541    65
  259.  
  260. test parse-7.1 {backslash substitution} {
  261.     set a "\a\c\n\]\}"
  262.     string length $a
  263. } 5
  264. test parse-7.2 {backslash substitution} {
  265.     set a {\a\c\n\]\}}
  266.     string length $a
  267. } 10
  268. test parse-7.3 {backslash substitution} {
  269.     set a "abc\
  270. def"
  271.     set a
  272. } {abc def}
  273. test parse-7.4 {backslash substitution} {
  274.     set a {abc\
  275. def}
  276.     set a
  277. } {abc def}
  278. test parse-7.5 {backslash substitution} {
  279.     set msg {}
  280.     set a xxx
  281.     set error [catch {if {24 < \
  282.     35} {set a 22} {set \
  283.         a 33}} msg]
  284.     list $error $msg $a
  285. } {0 22 22}
  286. test parse-7.6 {backslash substitution} {
  287.     eval "concat abc\\"
  288. } "abc\\"
  289. test parse-7.7 {backslash substitution} {
  290.     eval "concat \\\na"
  291. } "a"
  292. test parse-7.8 {backslash substitution} {
  293.     eval "concat x\\\n       \na"
  294. } "x a"
  295. test parse-7.9 {backslash substitution} {
  296.     eval "concat \\x"
  297. } "x"
  298.  
  299. # Semi-colon.
  300.  
  301. test parse-8.1 {semi-colons} {
  302.     set b 0
  303.     getArgs a;set b 2
  304.     set argv
  305. } a
  306. test parse-8.2 {semi-colons} {
  307.     set b 0
  308.     getArgs a;set b 2
  309.     set b
  310. } 2
  311. test parse-8.3 {semi-colons} {
  312.     getArgs a b ; set b 1
  313.     set argv
  314. } {a b}
  315. test parse-8.4 {semi-colons} {
  316.     getArgs a b ; set b 1
  317.     set b
  318. } 1
  319.  
  320. # The following checks are to ensure that the interpreter's result
  321. # gets re-initialized by Tcl_Eval in all the right places.
  322.  
  323. test parse-9.1 {result initialization} {concat abc} abc
  324. test parse-9.2 {result initialization} {concat abc; proc foo {} {}} {}
  325. test parse-9.3 {result initialization} {concat abc; proc foo {} $a} {}
  326. test parse-9.4 {result initialization} {proc foo {} [concat abc]} {}
  327. test parse-9.5 {result initialization} {concat abc; } abc
  328. test parse-9.6 {result initialization} {
  329.     eval {
  330.     concat abc
  331. }} abc
  332. test parse-9.7 {result initialization} {} {}
  333. test parse-9.8 {result initialization} {concat abc; ; ;} abc
  334.  
  335. # Syntax errors.
  336.  
  337. test parse-10.1 {syntax errors} {catch "set a \{bcd" msg} 1
  338. test parse-10.2 {syntax errors} {
  339.     catch "set a \{bcd" msg
  340.     set msg
  341. } {missing close-brace}
  342. test parse-10.3 {syntax errors} {catch {set a "bcd} msg} 1
  343. test parse-10.4 {syntax errors} {
  344.     catch {set a "bcd} msg
  345.     set msg
  346. } {missing "}
  347. test parse-10.5 {syntax errors} {catch {set a "bcd"xy} msg} 1
  348. test parse-10.6 {syntax errors} {
  349.     catch {set a "bcd"xy} msg
  350.     set msg
  351. } {extra characters after close-quote}
  352. test parse-10.7 {syntax errors} {catch "set a {bcd}xy" msg} 1
  353. test parse-10.8 {syntax errors} {
  354.     catch "set a {bcd}xy" msg
  355.     set msg
  356. } {extra characters after close-brace}
  357. test parse-10.9 {syntax errors} {catch {set a [format abc} msg} 1
  358. test parse-10.10 {syntax errors} {
  359.     catch {set a [format abc} msg
  360.     set msg
  361. } {missing close-bracket}
  362. test parse-10.11 {syntax errors} {catch gorp-a-lot msg} 1
  363. test parse-10.12 {syntax errors} {
  364.     catch gorp-a-lot msg
  365.     set msg
  366. } {invalid command name: "gorp-a-lot"}
  367. test parse-10.13 {syntax errors} {
  368.     set a [concat {a}\
  369.  {b}]
  370.     set a
  371. } {a b}
  372. test parse-10.14 {syntax errors} {catch "concat \{a\}\\\n{b}" msg} 1
  373. test parse-10.15 {syntax errors} {
  374.     catch "concat \{a\}\\\n{b}" msg
  375.     set msg
  376. } {extra characters after close-brace}
  377.  
  378. # Long values (stressing storage management)
  379.  
  380. set a {1111 2222 3333 4444 5555 6666 7777 8888 9999 aaaa bbbb cccc dddd eeee ffff gggg hhhh iiii jjjj kkkk llll mmmm nnnn oooo pppp qqqq rrrr ssss tttt uuuu vvvv wwww xxxx yyyy zzzz AAAA BBBB CCCC DDDD EEEE FFFF GGGG HHHH}
  381.  
  382. test parse-11.1 {long values} {
  383.     string length $a
  384. } 214
  385. test parse-11.2 {long values} {
  386.     llength $a
  387. } 43
  388. test parse-1a1.3 {long values} {
  389.     set b "1111 2222 3333 4444 5555 6666 7777 8888 9999 aaaa bbbb cccc dddd eeee ffff gggg hhhh iiii jjjj kkkk llll mmmm nnnn oooo pppp qqqq rrrr ssss tttt uuuu vvvv wwww xxxx yyyy zzzz AAAA BBBB CCCC DDDD EEEE FFFF GGGG HHHH"
  390.     set b
  391. } $a
  392. test parse-11.4 {long values} {
  393.     set b "$a"
  394.     set b
  395. } $a
  396. test parse-11.5 {long values} {
  397.     set b [set a]
  398.     set b
  399. } $a
  400. test parse-11.6 {long values} {
  401.     set b [concat 1111 2222 3333 4444 5555 6666 7777 8888 9999 aaaa bbbb cccc dddd eeee ffff gggg hhhh iiii jjjj kkkk llll mmmm nnnn oooo pppp qqqq rrrr ssss tttt uuuu vvvv wwww xxxx yyyy zzzz AAAA BBBB CCCC DDDD EEEE FFFF GGGG HHHH]
  402.     string length $b
  403. } 214
  404. test parse-11.7 {long values} {
  405.     set b [concat 1111 2222 3333 4444 5555 6666 7777 8888 9999 aaaa bbbb cccc dddd eeee ffff gggg hhhh iiii jjjj kkkk llll mmmm nnnn oooo pppp qqqq rrrr ssss tttt uuuu vvvv wwww xxxx yyyy zzzz AAAA BBBB CCCC DDDD EEEE FFFF GGGG HHHH]
  406.     llength $b
  407. } 43
  408. test parse-11.8 {long values} {
  409.     set b
  410. } $a
  411. test parse-11.9 {long values} {
  412.     set a [concat 0000 1111 2222 3333 4444 5555 6666 7777 8888 9999 aaaa bbbb cccc dddd eeee ffff gggg hhhh iiii jjjj kkkk llll mmmm nnnn oooo pppp qqqq rrrr ssss tttt uuuu vvvv wwww xxxx yyyy zzzz AAAA BBBB CCCC DDDD EEEE FFFF GGGG HHHH IIII JJJJ KKKK LLLL MMMM NNNN OOOO PPPP QQQQ RRRR SSSS TTTT UUUU VVVV WWWW XXXX YYYY ZZZZ]
  413.     llength $a
  414. } 62
  415. set i 0
  416. foreach j [concat 0000 1111 2222 3333 4444 5555 6666 7777 8888 9999 aaaa bbbb cccc dddd eeee ffff gggg hhhh iiii jjjj kkkk llll mmmm nnnn oooo pppp qqqq rrrr ssss tttt uuuu vvvv wwww xxxx yyyy zzzz AAAA BBBB CCCC DDDD EEEE FFFF GGGG HHHH IIII JJJJ KKKK LLLL MMMM NNNN OOOO PPPP QQQQ RRRR SSSS TTTT UUUU VVVV WWWW XXXX YYYY ZZZZ] {
  417.     set test [string index 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ $i]
  418.     set test $test$test$test$test
  419.     set i [expr $i+1]
  420.     test parse-11.10 {long values} {
  421.     set j
  422.     } $test
  423. }
  424. test parse-11.10 {test buffer overflow in backslashes in braces} {
  425.     expr {"a" == {xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\101\101\101\101\101\101\101\101\101\101\101\101\101\101\101\101\101\101\101\101\101\101\101\101\101\101}}
  426. } 0
  427.